package com.allianz.webapps.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.allianz.frame.utils.PropertiesUtil;
import com.allianz.webapps.https.interfaces.appInitReadDocuments.AppInitReadDocuments;
import com.allianz.webapps.orderspojo.App;
import com.allianz.webapps.orderspojo.AppBase;
import com.fasterxml.jackson.databind.ObjectMapper;


@Controller
@RequestMapping(value="main")
public class AppInitReadDocumentsController {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AppInitReadDocuments initRead;
	
	@RequestMapping(value="initRead")
	@ResponseBody
	public App initRead(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		AppBase appBase=(AppBase)objectMapper.readValue(jsonParam, AppBase.class);
		String body=initRead.requestInitReadParam(appBase);
		JSONObject json=new JSONObject(body);
		//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		App app=null;
		String body1="";
		if (json.getString("Body").equals(null)) {
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1=json.getString("Body");
			app=objectMapper.readValue(body1, App.class);
			// 判断是否是在windows环境下
			/*String os = System.getProperty("os.name");
			if(os.toLowerCase().startsWith("win")){  
				String serverPath= PropertiesUtil.getResourcePath();
				app.getAppBase().setIllustrationUrl(serverPath+"private/pdf/0021A0001555/bbb.pdf"); 
			} */
		}
		return app;
		
	}
	
}
